package com.autonavi.abs.common.util;

import com.autonavi.abs.common.constant.DateTimeConstant;
import org.joda.time.DateTime;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author wwy
 * @version V1.0
 * <p>Description: 时间日期工具类</p>
 * @date 2020-07-20 19:11
 */
public class DateTimeUtils {

    /**
     * 增加秒
     *
     * @param date
     * @param seconds
     * @return
     */
    public static Date addSeconds(Date date, int seconds) {
        DateTime dateTime = new DateTime(date.getTime()).plusSeconds(seconds);
        return dateTime.toDate();
    }

    /**
     * 增加分钟
     *
     * @param date
     * @param minutes
     * @return
     */
    public static Date addMinutes(Date date, int minutes) {
        DateTime dateTime = new DateTime(date.getTime()).plusMinutes(minutes);
        return dateTime.toDate();
    }

    /**
     * 增加小时
     *
     * @param date
     * @param hours
     * @return
     */
    public static Date addHours(Date date, int hours) {
        DateTime dateTime = new DateTime(date.getTime()).plusHours(hours);
        return dateTime.toDate();
    }

    /**
     * 增加天
     *
     * @param date
     * @param days
     * @return
     */
    public static Date addDays(Date date, int days) {
        DateTime dateTime = new DateTime(date.getTime()).plusDays(days);
        return dateTime.toDate();
    }

    /**
     * 增加月
     *
     * @param date
     * @param months
     * @return
     */
    public static Date addMonths(Date date, int months) {
        DateTime dateTime = new DateTime(date.getTime()).plusMonths(months);
        return dateTime.toDate();
    }

    /**
     * 增加年
     *
     * @param date
     * @param years
     * @return
     */
    public static Date addYears(Date date, int years) {
        DateTime dateTime = new DateTime(date.getTime()).plusYears(years);
        return dateTime.toDate();
    }

    /**
     * 格式化时间
     *
     * @param date
     * @param format
     * @return
     */
    public static String formatDate(Date date, String format) {
        DateTime dateTime = new DateTime(date.getTime());
        return dateTime.toString(format);
    }

    /**
     * 格式化当前时间
     *
     * @param format
     * @return
     */
    public static String formatNow(String format) {
        DateTime dateTime = new DateTime();
        return dateTime.toString(format);
    }

    /**
     * 获取utc时间格式
     *
     * @param date
     * @return
     */
    public static String getUTC8Str(Date date) {
        return formatDate(date, DateTimeConstant.PATTERN_UTC_8);
    }

    /**
     * 字符串转时间
     *
     * @param timeStr
     * @param pattern
     * @return
     */
    public static Date parseDate(String timeStr, String pattern) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        try {
            Date date = dateFormat.parse(timeStr);
            return date;
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 获取一天中开始时间
     *
     * @param date
     * @return
     */
    public static Date getStartTime(Date date) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        return calendar.getTime();

    }

    /**
     * 获取一天中最后时间
     *
     * @param date
     * @return
     */
    public static Date getEndTime(Date date) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);

        return calendar.getTime();

    }

}
